<?php
namespace App\Module\Admin\Service;

use App\Constants\Constant;
use App\Model\Child;
use App\Model\Foot;
use App\Model\Payment;
use App\Model\Teacher;
use App\Model\User;
use App\Model\UserScore;
use App\Model\UserScoreDetail;
use Carbon\Carbon;

class StatisService {

    public function index(){
        $pay = Payment::query()
            ->where("pay_status",Constant::PAY_STATUS_SUCCESS)
            ->whereYear("add_time",Carbon::now()->year)
            ->whereMonth("add_time",Carbon::now()->month)
            ->get();

        $total_pay_month = $pay->sum("total_fee");
        $total_pay_count_month = $pay->count();

        $total_user_count = User::query()->count();
        $total_teacher_count = Teacher::query()->count();
        $total_child_count = Child::query()->count();

        $total_foot_count = Foot::query()->count();
        $total_publish_count = Foot::query()->where("is_public",Constant::FOOT_PUBLIC)->count();
        $total_private_count = Foot::query()->where("is_public",Constant::FOOT_PRIVATE)->count();
        $foot = Foot::query()->whereDate("add_time",Carbon::now()->toDateString())->get();
        $publish_foot_count_day = $foot->sum(function($item){
            return $item->is_public == Constant::FOOT_PUBLIC ? 1 : 0;
        });
        $private_foot_count_day = $foot->count() - $publish_foot_count_day;

        $total_score = UserScore::query()->sum("total");
        $total_consume_score = UserScore::query()->sum("consume");
        $score_day = UserScoreDetail::query()
            ->where("direction",Constant::DIRECTION_INC)
            ->whereDate("add_time",Carbon::now()->toDateString())->sum("score");
        $consume_day = UserScoreDetail::query()
            ->where("direction",Constant::DIRECTION_DEC)
            ->whereDate("add_time",Carbon::now()->toDateString())->sum("score");

        return [
            'total_pay_month'=>round($total_pay_month,2),  //本月总支付金额
            'total_pay_count_month'=>$total_pay_count_month, //本月总支付笔数
            'total_user_count'=>$total_user_count, //总用户数
            'total_teacher_count'=>$total_teacher_count, //总老师数
            'total_child_count'=>$total_child_count, //总孩子数
            'total_foot_count'=>$total_foot_count, //总足迹数
            'total_publish_count'=>$total_publish_count, //足迹总公开数
            'total_private_count'=>$total_private_count, //足迹总私有数
            'publish_foot_count_day'=>$publish_foot_count_day, //足迹今日公开
            'private_foot_count_day'=>$private_foot_count_day, //足迹今日私有数
            'total_score'=>$total_score, //总获得积分
            'total_consume_score'=>$total_consume_score, //总消耗积分
            'score_day'=>$score_day, //今日获得积分
            'consume_day'=>$consume_day, //今日消耗积分
        ];
    }
}